package gs.android.bingomathfree;

import java.util.GregorianCalendar;

import android.app.Activity;
import android.app.Dialog;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.net.Uri;
import android.view.View;
import android.view.Window;
import android.widget.Button;

public class RateDialog extends Dialog {
	private SharedPreferences sp;
	private Button rate, later;
	private Activity activity;
	private Dialog d;

	public RateDialog(Context context) {
		super(context);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.rate);
		
		activity = (Activity) context;
		d = this;
		
		sp = activity.getSharedPreferences("bingo", Activity.MODE_PRIVATE);
		
		rate = (Button) findViewById(R.id.btnRate);
		rate.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Uri uri = Uri.parse("market://details?id=" + activity.getPackageName());
				Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
				try {
					Editor editor = sp.edit();
					editor.putBoolean("rated", true);
					editor.commit();
					activity.startActivity(goToMarket);
					d.dismiss();
				} catch (ActivityNotFoundException e) {
					//Toast.makeText(this, "Couldn't launch the market", Toast.LENGTH_LONG).show();
				}
			}
		});
		
		later = (Button) findViewById(R.id.btnLater);
		later.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Editor editor = sp.edit();
				editor.putLong("rate_time", GregorianCalendar.getInstance().getTimeInMillis() + (1000 * 60 * 60 * 24 * 2));
				editor.commit();
				d.dismiss();
			}
		});
	}
}